using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParallexBackGround : MonoBehaviour
{
    private GameObject cam;
    [SerializeField] private float parallexEffect;
    private float xPosition;
    private float lastCameraPostionX;
    private float length;
    void Start()
    {
        cam = GameObject.Find("Main Camera");
        xPosition = transform.position.x;
        lastCameraPostionX = cam.transform.position.x;
        length = GetComponent<SpriteRenderer>().bounds.size.x;
    }

    // Update is called once per frame
    void Update()
    {
        float nowCameraPostionX = cam.transform.position.x;
        float moveDistance = nowCameraPostionX - lastCameraPostionX;
        transform.position += new Vector3(moveDistance * parallexEffect, 0, 0);
        if (Mathf.Abs(cam.transform.position.x - transform.position.x) > length)
        {
            transform.position=new Vector3(cam.transform.position.x,cam.transform.position.y, transform.position.z);
        }
        lastCameraPostionX = nowCameraPostionX;
        
    }
}
